From 5d179b7561692d372481dec35002f96fee053e01 Mon Sep 17 00:00:00 2001 From: Alastair Tse Date: Fri, 13 Oct 2006 15:34:01 +0100 Subject: [PATCH] [XEND] XendVDI saves configuration on change. Added a base class called AutoSaveObject that will attempt to call save_config() if any attribute in the object changes. It isn't particularly efficient, but we do not expect VDI to change much. Signed-off-by: Alastair Tse --- tools/python/scripts/xapi.py | 12 ++++++++++++ tools/python/xen/xend/XendVDI.py | 26 +++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/tools/python/scripts/xapi.py b/tools/python/scripts/xapi.py index e44663bd1b..5b0d571324 100644 --- a/tools/python/scripts/xapi.py +++ b/tools/python/scripts/xapi.py @@ -37,6 +37,7 @@ COMMANDS = { 'sr-list': ('', 'List all SRs'), 'vbd-create': (' ', 'Create VBD attached to domname'), 'vdi-list' : ('', 'List all VDI'), + 'vdi-rename': (' ', 'Rename VDI'), 'vdi-delete': ('', 'Delete VDI'), 'vif-create': (' ', 'Create VIF attached to domname'), @@ -303,6 +304,17 @@ def xapi_vdi_delete(*args): print 'Deleting VDI %s' % vdi_uuid result = execute(server.VDI.destroy, session, vdi_uuid) print 'Done.' + +def xapi_vdi_rename(*args): + server, session = _connect() + if len(args) < 2: + raise OptionError('Not enough arguments') + + vdi_uuid = args[0] + vdi_name = args[1] + print 'Renaming VDI %s to %s' % (vdi_uuid, vdi_name) + result = execute(server.VDI.set_name_label, session, vdi_uuid, vdi_name) + print 'Done.' # diff --git a/tools/python/xen/xend/XendVDI.py b/tools/python/xen/xend/XendVDI.py index 652585adcc..c283a3a2ed 100644 --- a/tools/python/xen/xend/XendVDI.py +++ b/tools/python/xen/xend/XendVDI.py @@ -27,7 +27,25 @@ from xmlrpclib import dumps, loads KB = 1024 MB = 1024 * 1024 -class XendVDI: +class AutoSaveObject(object): + + def __init__(self): + self.cfg_path = None + self.auto_save = True + object + + def save_config(self, cfg_file = None): + raise NotImplementedError() + + def __setattr__(self, name, value): + """A very simple way of making sure all attribute changes are + flushed to disk. + """ + object.__setattr__(self, name, value) + if name != 'auto_save' and getattr(self, 'auto_save', False): + self.save_config() + +class XendVDI(AutoSaveObject): """Generic Xen API compatible VDI representation. @cvar SAVED_CFG: list of configuration attributes to save. @@ -60,20 +78,20 @@ class XendVDI: self.read_only = False self.type = "system" - self.cfg_path = None - def load_config_dict(self, cfg): """Loads configuration into the object from a dict. @param cfg: configuration dict @type cfg: dict """ + self.auto_save = False for key in self.SAVED_CFG: if key in cfg: if key in self.SAVED_CFG_INT: setattr(self, key, int(cfg[key])) else: setattr(self, key, cfg[key]) + self.auto_save = True def load_config(self, cfg_path): """Loads configuration from an XMLRPC parameter format. @@ -128,10 +146,12 @@ class XendQCOWVDI(XendVDI): def __init__(self, uuid, sr_uuid, qcow_path, image_path, cfg_path, vsize, psize): XendVDI.__init__(self, uuid, sr_uuid) + self.auto_save = False self.qcow_path = qcow_path self.image_path = image_path self.cfg_path = cfg_path self.physical_utilisation = psize self.virtual_size = vsize self.sector_size = 1 + self.auto_save = True -- 2.30.2